#include "StdAfx.h"

bool ADTChunkLoader::execute() {
	mMesh.Load(rlVertices, (VertexFMT)Vertex3FCNT2::FVF, 145, 256, (ui16*)ADTChunk::triangleIndices);
	mIsFinished = true;
	return true;
}

void ADTLoader::addTextures(std::vector<std::string>& textureNames) {
	MUTEX_LOCK(mTextureLock);
	std::for_each(textureNames.begin(), textureNames.end(),
		[this](std::string& tex) { mTextureNames.push(tex); } );	
}

bool ADTLoader::execute() {
	mTextureLock.Acquire();
	for(ui32 i = 0; i < 10 && mTextureNames.size(); ++i) {
		std::stringstream strm;
		strm << "Loading texture '" << mTextureNames.back() << "'...";
		Hud::GetInstance()->SetText(strm.str(), 2000);
		mTextures.push_back(sTexture.GetTexture(mTextureNames.back()));
		mTextureNames.pop();
	}

	bool shouldQuit = mTextureNames.size() != 0;
	mTextureLock.Release();
	if(shouldQuit)
		return false;

	mIndexLock.Acquire();
	for(ui32 i = 0; i < 20 && mIndicesToLoad.size(); ++i) {
		auto itr = mIndicesToLoad.begin();
		ui8 index = *itr;
		//std::stringstream strm;
		//strm << "Loading ADT-Chunk " << index % 16 << "/" << index / 16 << "...";
		//Hud::GetInstance()->SetText(strm.str(), 2000);

		if(mChunkLoaders[index].execute() == true)
			mIndicesToLoad.erase(itr);
	}

	shouldQuit = mIndicesToLoad.size() == 0;
	mIndexLock.Release();

	return shouldQuit;
}